/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.logger;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * User to format log messages
 * @author Ahsan
 */
public class LogFormatter extends Formatter
{

	 private static final SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	 
	 @Override
	 public String format(LogRecord record)
	 {
		  StringBuffer logBuffer = new StringBuffer();
		  
		  // Add the time to the log message
		  logBuffer.append (calculateTime (record.getMillis()));
		  		  
		  logBuffer.append (" [");
		  
		  // Add the class name to the log message
		  logBuffer.append(record.getSourceClassName());
		  logBuffer.append(".");
		  
		  // Add the method name to the log message
		  logBuffer.append(record.getSourceMethodName());
		  logBuffer.append ("] ");
		  
		  logBuffer.append ("[");
		  
		  //Add the  level to the log message
		  logBuffer.append (record.getLevel().toString());
		  logBuffer.append ("] ");
		  
		  // Add the message
		  logBuffer.append (this.formatMessage(record));
		  
		  // Add new line character
		  logBuffer.append ("\r\n");
		  
		  return logBuffer.toString();
		  
	 }
	 
	 public String calculateTime (long millis)
	 {
		  Date resultDate = new Date(millis);
		  return logDateFormat.format(resultDate);
	 }
	 
}